package com.cat.slidingWindow;

/**
 * @author 曲大人的喵
 * @description https://leetcode.cn/problems/get-equal-substrings-within-budget/https://leetcode.cn/problems/get-equal-substrings-within-budget/
 * @create 2025/9/17 12:16
 * @since JDK17
 */

public class Solution36 {
    public int equalSubstring(String s, String t, int maxCost) {
        char[] arr1 = s.toCharArray(), arr2 = t.toCharArray();
        int n = arr2.length, cost = 0, ans = 0;
        for (int l = 0, r = 0; r < n; r++) {
            cost += Math.abs(arr1[r] - arr2[r]);    // 转化当前字符
            while (cost > maxCost) {
                cost -= Math.abs(arr1[l] - arr2[l]);
                l++;
            }
            ans = Math.max(ans, r - l + 1);
        }
        return ans;
    }
}
